package report

import (
	"context"
	"sale-admin/internal/models"
	"strings"
	"time"

	"192.168.1.75/go-pkg/errorx"
	"192.168.1.75/go-pkg/helper"
	"gorm.io/gorm"
)

type ReportTypeService struct{}

func (rt ReportTypeService) EventTypeList(ctx context.Context, db *gorm.DB, page, limit int) (data []models.MaEventType, count int64, err error) {
	tx := db.Model(&models.MaEventType{})
	tx.Count(&count)
	var res = []models.MaEventType{}
	helper.Gorm{}.Paginate(tx, page, limit).Find(&res)
	return res, count, nil
}

func (rt ReportTypeService) AddEventType(ctx context.Context, db *gorm.DB, name string) (err error) {
	if strings.TrimSpace(name) == "" {
		err = errorx.New("事件类型不能为空", -1)
		return
	}
	var InsertData models.MaEventType
	InsertData.Name = name
	InsertData.AddTime = time.Now()
	db.Model(&models.MaEventType{}).Create(&InsertData)
	return
}

func (rt ReportTypeService) DelEventType(ctx context.Context, db *gorm.DB, id string) (err error) {
	db.Model(&models.MaEventType{}).Where("id = ?", id).
		Delete(&models.MaEventType{})
	return
}
